package com.jht.bletool2java.activity.blepage.fragment.blecontrol;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.jht.bletool2java.BLEApplication;
import com.jht.bletool2java.activity.homepage.fragment.bledevice.bean.BLEDevice;
import com.jht.bletool2java.service.BLEService;

import java.util.List;
import java.util.Map;

public class BLEControlPresenter implements IBLEControl.Presenter {
    private static final String TAG = "BLEControlPresenter";

    private BLEService bleService;
    private IBLEControl.UI ui;
    private BLEDevice bleDevice;
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            /**
             * 1.连接蓝牙
             * 2.更新UI
             */
            BLEService.BleServiceBinder service1 = (BLEService.BleServiceBinder) service;
            bleService = service1.getService();
            if (bleDevice != null && bleDevice.getDevice() != null) {
                bleService.connectGatt(bleDevice.getDevice());
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
            bleService.disConnected();
            bleService = null;
        }

    };

    public BLEControlPresenter(IBLEControl.UI ui) {
        this.ui = ui;
    }

    /**
     * 1.启动服务
     * 2.绑定服务
     */
    @Override
    public void connectBLE(BLEDevice ble_device) {
        setBleDevice(ble_device);
        //startBLEService();
        bindBLEService();

    }

    @Override
    public void closeConnect(){
        Context bleApplicationContext = BLEApplication.getBLEApplicationContext();
        bleApplicationContext.unbindService(conn);
    }


    private void startBLEService() {
        Context bleApplicationContext = BLEApplication.getBLEApplicationContext();
        Intent intent = new Intent(bleApplicationContext, BLEService.class);
        bleApplicationContext.startService(intent);
    }

    private void bindBLEService() {
        Context bleApplicationContext = BLEApplication.getBLEApplicationContext();
        Intent intent = new Intent(bleApplicationContext, BLEService.class);
        bleApplicationContext.bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }


    public BLEDevice getBleDevice() {
        return bleDevice;
    }

    public void setBleDevice(BLEDevice bleDevice) {
        this.bleDevice = bleDevice;
    }

    @Override
    public List<BluetoothGattService> getAllServices() {
        return bleService.getAllServices();
    }

    @Override
    public List<BluetoothGattCharacteristic> getAllCharacteristics() {
        return bleService.getAllCharacteristics();
    }

    @Override
    public Map<String, List<BluetoothGattCharacteristic>> getServicesCharacteristicsMap(){
        return bleService.getServicesCharacteristicsMap();
    }
    @Override
    public boolean readGatt(BluetoothGattCharacteristic characteristic){
        return bleService.readGatt(characteristic);
    }

    @Override
    public boolean writeGatt(BluetoothGattCharacteristic characteristic,byte[] data){
        return bleService.writeGatt(characteristic,data);
    }
    @Override
    public boolean enableNotify(BluetoothGattCharacteristic characteristic,boolean enable){
        if (enable){
            return bleService.enableNotificationOrIndication(characteristic);
        }else {
            return bleService.disEnableNotificationOrIndication(characteristic);
        }
    }

    @Override
    public boolean enableIndication(BluetoothGattCharacteristic characteristic,boolean enable){
        if (enable){
            return bleService.enableNotificationOrIndication(characteristic);
        }else {
            return bleService.disEnableNotificationOrIndication(characteristic);
        }
    }

}
